<?php

require_once(WSB_MODULES_PATH . 'media/Callback.php');
require_once(WSB_MODULES_PATH . 'media/Filesystem.php');
require_once(WSB_MODULES_PATH . 'media/Url.php');

class Media_UploadController extends Zend_Controller_Action
{
	public function init()
	{
		parent::init(); // immer zuerst!
		$navigation = $this->_helper->getHelper('Navigation');
		$navigation->add(Media_Url::fileListUrl(), 'Dateien');
		$navigation->add(Media_Url::uploadListUrl(), 'Upload');
	}

	public function indexAction()
	{

		$messages = array();
		$i = 10;

		// Uploadvorgang
		if(key_exists('file', $_FILES)) {
			$config = Zend_Registry::get('Zend_Config');
			for($j=0; $j<$i; $j++) {
				$name = $_FILES['file']['name'][$j];
				$type = $_FILES['file']['type'][$j];
				$tmpName = $_FILES['file']['tmp_name'][$j];
				$error = $_FILES['file']['error'][$j];
				$size = $_FILES['file']['size'][$j];
				if(intval($size) > 0) {
					$nameFiltered = Media_Filesystem::filterFilename($name);
					$dstFile = $this->_getRequestedPath() . '/' . $nameFiltered;
					if(!is_file($dstFile)) {
						$messages[] = sprintf("'%s' was uploaded successfully", basename($dstFile));
						move_uploaded_file($tmpName, $dstFile);
						umask(0);
						chmod($dstFile, $config->file->fileCreateMask);
						WSB_Log::info(sprintf('Uploaded file %s', $nameFiltered));
					} else {
						$messages[] = sprintf("'%s' already exists", basename($dstFile));
					}
				}
			}
		}

		$fileinfo = new WSB_Fileinfo($this->_getRequestedPath());

		$this->view->i = $i;
		$this->view->messages = $messages;
		$this->view->folder = $fileinfo->getRelativeMediaPath();
		$this->view->breadcrumb = Media_Filesystem::buildBreadcrumb($fileinfo->getRelativePath());
		$this->render();

		$this->_renderFolderTree();
	}

	public function mkdirAction()
	{
		$request = $this->getRequest();
		$folder = $request->getPost('folder');
		$dirname = $request->getPost('dirname');
		if(preg_match('/^[^\/\\\\":*?<>|]+$/', $dirname)) {
			$dirname = Media_Filesystem::filterDirname($dirname);
			$newDir = $this->_getRequestedPath() . '/' . $dirname;
			if(!is_dir($newDir)) {
				$config = Zend_Registry::get('Zend_Config');
				$mode = $config->file->folderCreateMask;
				umask(0);
				mkdir($newDir, $mode);
				WSB_Log::info(sprintf('Created folder %s', $folder . '/' . $dirname));
			}
		}
		$url = Media_Url::uploadListUrl($folder);
		$this->_helper->Redirect($url);
	}

	private function _renderFolderTree()
	{
		$path = Media_Filesystem::getMediaFolderPath();
		$tree = new WSB_Dhtml_FolderTree($path, 2);
		$tree->setPath($this->_getRequestedPath());
		$this->view->folderTree = $tree;
		$this->render('folder-tree', 'left');
	}

	protected function _getRequestedPath()
	{
		$request = $this->getRequest();
		$path = Media_Filesystem::getMediaFolderPath();
		$folder = $request->get('folder', '');

		// Auf Null pruefen (dies ist nur der Fall, wenn das Modul ohne
		// Parameter aufgerufen wird).
		// Falls kein Parameter uebergeben wird, hole den Pfad aus der Session,
		// aber nur, wenn ein gueltiger Pfad in der Session gespeichert ist.

		//$session = $this->getHelper('Session');
		// Gleicher Session Namespace im Module Media
		$session = new Zend_Session_Namespace('MediaModule');

		if(is_null($folder) && isset($session->path) && (is_dir($session->path))) {
			return $session->path;
		} else {
			$folder = Media_Filesystem::sanitizePath($folder);
			if($folder != '') {
				$path .= '/' . $folder;
			}
			$session->path = $path;
			return $path;
		}

	}

}